﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class PermissionController : Controller
    {
        //
        // GET: /Permission/
        public ActionResult PermissionList()
        {
            return View();
        }
        public ActionResult AssignAllPerm()
        {
            return View();
        }
        public ActionResult List(View_BD_Permission permission)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field="Parent",Operator="!=",Value="0"});
            Expression<Func<View_BD_Permission, bool>> exp = SearchExpression.FindByGroup<View_BD_Permission>(permission, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = BLLSession.Current.View_BD_PermissionBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, o => o.Id, true);
            count = BLLSession.Current.View_BD_PermissionBLL.GetCount(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }

        public ActionResult EditPermission(BD_Permission permission)
        {
            permission.AddTime = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            permission.Clerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_PermissionBLL.ModifyBy(permission, p => p.Id == permission.Id, "Parent", "Name", "OrderField", "LinkUrl", "Note", "AddTime", "IsShow", "Clerk", "IsShortCut", "ShortCutPic", "IsFatherNode");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        public ActionResult DelPermission(int Id)
        {
            BLLSession.Current.BD_PermissionBLL.DelBy(d => d.Id == Id);
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        public ActionResult AddPermission(BD_Permission permission)
        {
            permission.AddTime = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            permission.Clerk = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_PermissionBLL.Add(permission);
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        public ActionResult GetUserBtnPermission(string url)
        {
            List<BD_btnPermission> btnPerm = new List<BD_btnPermission>();
            string shortUrl = url.Split('?')[0];
            BD_Permission perm = OperateContext.SessPermission.Where(p => p.LinkUrl == shortUrl).FirstOrDefault();
            if (perm != null)
            {
                btnPerm = OperateContext.SessbtnPermission.Where(b => b.perId == perm.Id).OrderBy(b => b.Order).ToList();
            }
            return Json(btnPerm);
        }
        public JsonResult GetParentPerm()
        {
            var list=DALSession.Current.BD_PermissionDAL.GetListBy(p => p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name });
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public JsonResult GetSubPerm()
        {
            var list = DALSession.Current.BD_PermissionDAL.GetListBy(p => p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name });
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        //根据模块名称获取非父权限
        public JsonResult GetSubPermByModelName()
        {
            string modelName = Request.QueryString["modelName"];

            var father1 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent != 0 && p.ParentName.Contains(modelName) && p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name }).ToList();

            var father2 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent != 0 && p.ParentName.Contains(modelName) && p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name }).ToList();
            foreach (var item in father2)
            {
                var sub1 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent == item.value && p.IsFatherNode == "1").Select(p => new { value = p.Id, text = p.Name }).ToList();
                var sub2 = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent == item.value && p.IsFatherNode == "0").Select(p => new { value = p.Id, text = p.Name }).ToList();
                father1.AddRange(sub1);
                foreach (var child in sub2)
                {
                    var son = DALSession.Current.View_BD_PermissionDAL.GetListBy(p => p.Parent == child.value).Select(p => new { value = p.Id, text = p.Name }).ToList();
                    father1.AddRange(son);
                }

            }
            return Json(father1, JsonRequestBehavior.AllowGet);
        }
        public void AddBtn()
        {
            int id = int.Parse(Request.Form["ID"]);
            //添加页面权限
            BD_RolePermission per = new BD_RolePermission();
            per.RId = 1;
            per.PId = id;
            BLLSession.Current.BD_RolePermissionBLL.Add(per);
            //添加查询按钮及权限
            BD_btnPermission search = new BD_btnPermission() { perId = id, Name = "查询", Ico = "fa fa-search",Order="1",AddTime=DateTime.Now.ToString("yyyy-MM-dd"),btnId="btnSearch",btnFunc="funcSearch" };
            int btnid=int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(search));
            BD_RoleBtnPermission btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);

            BD_btnPermission add = new BD_btnPermission() { perId = id, Name = "添加", Ico = "fa fa-plus", Order = "2", AddTime = DateTime.Now.ToString("yyyy-MM-dd"), btnId = "btnAdd", btnFunc = "funcAdd" };
            btnid = int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(add));
            btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);

            BD_btnPermission edit = new BD_btnPermission() { perId = id, Name = "修改", Ico = "fa fa-pencil", Order = "3", AddTime = DateTime.Now.ToString("yyyy-MM-dd"), btnId = "btnEdit", btnFunc = "funcEdit" };
            btnid = int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(edit));
            btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);

            BD_btnPermission save = new BD_btnPermission() { perId = id, Name = "保存", Ico = "fa fa-save", Order = "4", AddTime = DateTime.Now.ToString("yyyy-MM-dd"), btnId = "btnSave", btnFunc = "funcSave" };
            btnid = int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(save));
            btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);

            BD_btnPermission remove = new BD_btnPermission() { perId = id, Name = "删除", Ico = "fa fa-trash", Order = "5", AddTime = DateTime.Now.ToString("yyyy-MM-dd"), btnId = "btnRemove", btnFunc = "funcRemove" };
            btnid = int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(remove));
            btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);

            BD_btnPermission cancel = new BD_btnPermission() { perId = id, Name = "取消编辑", Ico = "fa fa-reply", Order = "6", AddTime = DateTime.Now.ToString("yyyy-MM-dd"), btnId = "btnCancel", btnFunc = "funcCancel" };
            btnid = int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(cancel));
            btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);

            BD_btnPermission reload = new BD_btnPermission() { perId = id, Name = "重置", Ico = "fa fa-retweet", Order = "7", AddTime = DateTime.Now.ToString("yyyy-MM-dd"), btnId = "btnReload", btnFunc = "funcReload" };
            btnid = int.Parse(BLLSession.Current.BD_btnPermissionBLL.AddModelReturnId(reload));
            btnper = new BD_RoleBtnPermission();
            btnper.RId = 1;
            btnper.BId = btnid;
            BLLSession.Current.BD_RoleBtnPermissionBLL.Add(btnper);
            Response.Write(JsonHelper.toDefaultMsg(true));
        }
    }
}
